package com.rongshu.sendmessage.util.upData;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.DownloadListener;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.rongshu.sendmessage.R;
import com.rongshu.sendmessage.bean.UpDataVersionBean;

import java.util.List;

import static android.view.View.GONE;

public class UpDataDialog {
    private final int state;//2强制1是可以返回
    private final UpDataVersionBean upData;
    private View inflate;
    private Context mContext;
    private BottomSheetDialog bsd1;
    private View tv_title;
    private TextView tv_banben_number,tv_last_time,tv_content;

    public UpDataDialog(int state, UpDataVersionBean upData){
        this.state=state;
        this.upData=upData;
    }
    private DownloadListener downloadListener=new DownloadListener() {
        @Override
        public void onDownloadStart(String url, String userAgent, String contentDisposition,
                                    String mimetype, long contentLength) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                boolean hasInstallPermission = mContext.getPackageManager().canRequestPackageInstalls();
                if (!hasInstallPermission) {
                    startInstallPermissionSettingActivity();
                    return;
                }
            }
            udpate(url);

        }
    };

    /**
     * 跳转到设置-允许安装未知来源-页面
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    private void startInstallPermissionSettingActivity() {
        //注意这个是8.0新API
        Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);
    }


    public void udpate(String url){
        UpdateManager manager = new UpdateManager(mContext, mContext,url);
        // 检查软件更新
        manager.checkUpdate();
    }
    public View showDialog(Context context) {
        //自定义dialog显示布局
        inflate = LayoutInflater.from(context).inflate(R.layout.hy_updata_view, null);
        mContext=context;
        initView(inflate);
        //自定义dialog显示风格
        bsd1=new BottomSheetDialog(mContext);
        inflate.post(new Runnable() {
            @Override
            public void run() {
                BottomSheetBehavior behavior= BottomSheetBehavior.from(bsd1.findViewById(R.id.design_bottom_sheet));
                behavior.setHideable(false);//此处设置表示禁止BottomSheetBehavior的执行
            }
        });
        bsd1.setContentView(inflate);
        bsd1.getWindow().findViewById(R.id.design_bottom_sheet)
                .setBackgroundResource(android.R.color.transparent);
        bsd1.setCanceledOnTouchOutside(false);
        bsd1.show();
        bsd1.setCanceledOnTouchOutside(false);
        bsd1.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if(mdialogLinstner!=null){
                    mdialogLinstner.closeLinstner(10);
                }
            }
        });
        return inflate;
    }
    public boolean isShow(){
        return bsd1.isShowing();
    }
    private void initView(View inflate) {
        tv_banben_number =  inflate.findViewById(R.id.tv_banben_number);
        tv_banben_number.setText("版本号："+upData.getVersionName());
        tv_last_time =  inflate.findViewById(R.id.tv_last_time);
        tv_content =  inflate.findViewById(R.id.tv_content);
        tv_title =  inflate.findViewById(R.id.tv_title);
        if (state==1){
            tv_title.setVisibility(View.VISIBLE);
        }else{
            tv_title.setVisibility(GONE);
        }
        tv_title.setOnClickListener(view -> {
            if(mdialogLinstner!=null&&state==1){
                mdialogLinstner.closeLinstner(state);
            }
            bsd1.dismiss();
        });
        inflate.findViewById(R.id.iv_download).setOnClickListener(view->updata(upData.getAppURL()));
        inflate.findViewById(R.id.iv_download_1).setOnClickListener(view->updata(upData.getAppURL()));
    }
    //更新下载应用
    private void updata(String upDataUrl) {
        UpdateManager manager = new UpdateManager(mContext,null,upDataUrl);
        // 检查软件更新
        manager.checkUpdate();
    }

    public void setOnclickLisner(DialogCloseLinstner linstner) {
        mdialogLinstner=linstner;
    }
    DialogCloseLinstner mdialogLinstner;
    public interface DialogCloseLinstner{
        void closeLinstner(int status);
    }
}
